//
//  SINATeacherMyViewController.m
//  sinashebao
//
//  Created by  androidlongs on 2017/12/29.
//  Copyright © 2017年  androidlongs. All rights reserved.
//

#import "SINATeacherMyViewController.h"
#import "SINATeacherSettingViewController.h"
#import "SINATeacherSelectIdentsViewController.h"
#import "SINAMainMsgViewController.h"
#import "SINATecPersonInformationViewController.h"
#import "SINAUserTitleDetaileViewController.h"
#import "SINAUserHelperViewController.h"
#import "SINAHelpFeedbackViewController.h"

@interface SINATeacherMyViewController ()
//我的头像
@property (weak, nonatomic) IBOutlet UIImageView *myTitleImageView;

//姓名
@property (weak, nonatomic) IBOutlet UILabel *tecRealNameLabel;

//学校
@property (weak, nonatomic) IBOutlet UILabel *tecSchoolLabel;

//职称
@property (weak, nonatomic) IBOutlet UILabel *tecSchoolWorkLabel;
@property (weak, nonatomic) IBOutlet UIView *otherItemView;

//接收消息通知
@property (weak, nonatomic) IBOutlet UIView *msgView;
//个人资料
@property (weak, nonatomic) IBOutlet UIView *personInformationUiview;
@property (weak, nonatomic) IBOutlet UIView *informationDiviView;
@property (weak, nonatomic) IBOutlet UIView *helperDiviView;

@property (weak, nonatomic) IBOutlet UIImageView *inforIconImageView;
@property (weak, nonatomic) IBOutlet UILabel *inforTextLabel;
@property (weak, nonatomic) IBOutlet UIImageView *inforToRightImageView;


//帮助与反馈
@property (weak, nonatomic) IBOutlet UIView *helperAndBackView;
@property (weak, nonatomic) IBOutlet UIImageView *msgIconImageView;
@property (weak, nonatomic) IBOutlet UILabel *msgTextLabel;
@property (weak, nonatomic) IBOutlet UIImageView *msgToRightImageView;
@property (weak, nonatomic) IBOutlet UIImageView *helperIconImageView;
@property (weak, nonatomic) IBOutlet UILabel *helperTextLabel;
@property (weak, nonatomic) IBOutlet UIImageView *helperToRightImageView;



//设置
@property (weak, nonatomic) IBOutlet UIView *settingView;
@property (weak, nonatomic) IBOutlet UIImageView *settingIconImageView;
@property (weak, nonatomic) IBOutlet UILabel *settingTextLabel;
@property (weak, nonatomic) IBOutlet UIImageView *settingToRightImageView;


//用户相关信息
@property (weak, nonatomic) IBOutlet UIView *headerView;



@end

@implementation SINATeacherMyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self itemClickFunction];
    
    if (IS_IPHONE_X) {
        self.headerView.frame=CGRectMake(0, 0, mainW, 156);
        self.myTitleImageView.frame=CGRectMake(15, 66, 72, 72);
     self.tecRealNameLabel.frame=CGRectMake(CGRectGetMaxX(self.myTitleImageView.frame)+12, 64, mainW-CGRectGetMaxX(self.myTitleImageView.frame)-12*2, 21);
    } else {
        self.headerView.frame=CGRectMake(0, 0, mainW, 136);
        self.myTitleImageView.frame=CGRectMake(15, 46, 72, 72);
        self.tecRealNameLabel.frame=CGRectMake(CGRectGetMaxX(self.myTitleImageView.frame)+12, 44, mainW-CGRectGetMaxX(self.myTitleImageView.frame)-12*2, 21);
        
    }
    
    self.tecSchoolLabel.frame=CGRectMake(CGRectGetMaxX(self.myTitleImageView.frame)+12, CGRectGetMaxY(self.tecRealNameLabel.frame)+4, mainW-CGRectGetMaxX(self.myTitleImageView.frame)-12*2, 21);
    
    self.tecSchoolWorkLabel.frame=CGRectMake(CGRectGetMaxX(self.myTitleImageView.frame)+12, CGRectGetMaxY(self.tecSchoolLabel.frame)+4, mainW-CGRectGetMaxX(self.myTitleImageView.frame)-12*2, 21);
    
    
    self.msgView.frame=CGRectMake(0, CGRectGetMaxY(self.headerView.frame)+14, mainW, 45);
    
    self.msgIconImageView.frame=CGRectMake(15,13, 20, 18);
    self.msgTextLabel.frame=CGRectMake(CGRectGetMaxX(self.msgIconImageView.frame)+14, CGRectGetHeight(self.msgView.frame)/2-21/2, mainW-CGRectGetMaxX(self.msgIconImageView.frame)-14*2, 21);
    self.msgToRightImageView.frame=CGRectMake(mainW-12-7, CGRectGetHeight(self.msgView.frame)/2-7/2, 7, 13);
    
    self.otherItemView.frame=CGRectMake(0, CGRectGetMaxY(self.msgView.frame)+12, mainW, 135);
    
    
    self.personInformationUiview.frame=CGRectMake(0,0, mainW, 45);
    self.helperAndBackView.frame=CGRectMake(0, CGRectGetMaxY(self.personInformationUiview.frame), mainW, 45);
    
    
    self.informationDiviView.frame=CGRectMake(12, CGRectGetMaxY(self.personInformationUiview.frame), mainW-12*2,1);
    self.helperDiviView.frame=CGRectMake(12, CGRectGetMaxY(self.helperAndBackView.frame), mainW-12*2,1);
    self.settingView.frame=CGRectMake(0, CGRectGetMaxY(self.helperDiviView.frame), mainW,45);
    
    self.inforIconImageView.frame=CGRectMake(15,11, 18, 22);
    self.inforTextLabel.frame=CGRectMake(CGRectGetMaxX(self.inforIconImageView.frame)+14, CGRectGetHeight(self.msgView.frame)/2-21/2, mainW-CGRectGetMaxX(self.inforIconImageView.frame)-14*2, 21);
    self.inforToRightImageView.frame=CGRectMake(mainW-12-7, CGRectGetHeight(self.personInformationUiview.frame)/2-7/2, 7, 13);
    
    self.helperIconImageView.frame=CGRectMake(15,11, 18, 22);
    self.helperTextLabel.frame=CGRectMake(CGRectGetMaxX(self.helperIconImageView.frame)+14, CGRectGetHeight(self.helperAndBackView.frame)/2-21/2, mainW-CGRectGetMaxX(self.helperIconImageView.frame)-14*2, 21);
    self.helperToRightImageView.frame=CGRectMake(mainW-12-7, CGRectGetHeight(self.helperAndBackView.frame)/2-7/2, 7, 13);
    
    
    self.settingIconImageView.frame=CGRectMake(15,12, 20, 20);
    self.settingTextLabel.frame=CGRectMake(CGRectGetMaxX(self.settingIconImageView.frame)+14, CGRectGetHeight(self.settingView.frame)/2-21/2, mainW-CGRectGetMaxX(self.settingIconImageView.frame)-14*2, 21);
    self.settingToRightImageView.frame=CGRectMake(mainW-12-7, CGRectGetHeight(self.settingView.frame)/2-7/2, 7, 13);
    
    
    SINAUserModel *userModel = [[SINACommonUserHelper share] getUserModel];
    if (userModel!=nil) {
        NSString *univestiName = userModel.universityName;
        if (univestiName==nil||[univestiName isEqualToString:@""]) {
            //获取个人信息详情
            [self loadingUserInformationFunction];
        }
    }
    
    
    
}
-(void) itemClickFunction{
    
    //我的头像点击事件
    UITapGestureRecognizer *titleImageTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(alertTitleClickFunction)];
    //我的头像将点击事件添加到label上
    [self.myTitleImageView addGestureRecognizer:titleImageTapGestureRecognizer];
    // 我的头像label可被点击
    self.myTitleImageView.userInteractionEnabled = YES;
    
    //设置中心点击事件
    UITapGestureRecognizer *showSettingTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(settingClickFunction)];
    // 2. 将点击事件添加到label上
    [self.settingView addGestureRecognizer:showSettingTapGestureRecognizer];
    // 可以理解为设置label可被点击
    self.settingView.userInteractionEnabled = YES;
    
    //接收通知中心点击事件
    UITapGestureRecognizer *showMsgTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(msgPageClickFunction)];
    //接收通知中心点击事件添加到label上
    [self.msgView addGestureRecognizer:showMsgTapGestureRecognizer];
    //接收通知中心label可被点击
    self.msgView.userInteractionEnabled = YES;
    
    
    //个人资料点击事件
    UITapGestureRecognizer *personInfromationTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(presonInfromationClickFunction)];
    //个人资料点击事件添加到label上
    [self.personInformationUiview addGestureRecognizer:personInfromationTapGestureRecognizer];
    //个人资料label可被点击
    self.personInformationUiview.userInteractionEnabled = YES;
    
    
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.helperAndBackView andWithBlick:^(UIView *view) {
        if ([super checkApproveIdent]) {
            SINAHelpFeedbackViewController *controller =[[SINAHelpFeedbackViewController alloc]init];
            controller.helpFeedbackType = SINAHELPFEEDBACK_TEACHER_TYPE;
            controller.hidesBottomBarWhenPushed=YES;
            [self.navigationController pushViewController:controller animated:YES];
        }
       
    }];
    
}

//修改头像页面
- (void)alertTitleClickFunction{
    SINAUserTitleDetaileViewController
    *settingController = [[SINAUserTitleDetaileViewController alloc]initWithNibName:@"SINAUserTitleDetaileViewController" bundle:nil];
    settingController.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:settingController animated:YES];
}
//消息页面
- (void)msgPageClickFunction {
    SINAMainMsgViewController
    *msgController = [[SINAMainMsgViewController alloc]initWithNibName:@"SINAMainMsgViewController" bundle:nil];
    msgController.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:msgController animated:YES];
}

//个人资料页面
- (void)presonInfromationClickFunction {
    if ([super checkApproveIdent]) {
        SINATecPersonInformationViewController
        *msgController = [[SINATecPersonInformationViewController alloc]initWithNibName:@"SINATecPersonInformationViewController" bundle:nil];
        msgController.hidesBottomBarWhenPushed=YES;
        [self.navigationController pushViewController:msgController animated:YES];
    }
}

//跳转设置中心
-(void)settingClickFunction{
    
    SINATeacherSettingViewController * teacherSettingViewController = [[SINATeacherSettingViewController alloc]init];
    teacherSettingViewController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:teacherSettingViewController animated:YES];
}
-(void)viewWillAppear:(BOOL)animated{
//    self.navigationController.navigationBarHidden = YES;
    
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    
    SINAUserModel *userModel = [[SINACommonUserHelper share] getUserModel];
    if (userModel!=nil) {
        //设置昵称
        NSString *userNikName = userModel.userRealName;
        if (userNikName==nil||[userNikName isEqualToString:@""]||[userNikName isEqualToString:@"(null)"]) {
            NSString *phone = userModel.userPhone;
            if (phone==nil||[phone isEqualToString:@""]) {
                phone=@"姓名：未知";
            }else{
                NSString *themPhone = phone;
                phone = [NSString stringWithFormat:@"%@****%@",[themPhone substringToIndex:3],[themPhone substringToIndex:4]];
            }
            userNikName = phone;
        }
        self.tecRealNameLabel.text=userNikName;
        //个性签名
        NSString *sign =userModel.sign;
        if (sign==nil||sign.length==0) {
            sign=@"这个世界很美，不仿写个签名吧";
        }
        //self.userSignLabel.text = sign;
        
        //头像
        NSString *headerImageUrl = userModel.userTitleImageUrl;
        if (headerImageUrl!=nil&&headerImageUrl.length>1) {
            NSURL *imageUrl =[NSURL URLWithString:headerImageUrl];
            [self.myTitleImageView sd_setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"默认头像"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                DLog(@"加载头像完成");
            }];
        }
        
        NSString *univestiName = userModel.universityName;
        if (univestiName==nil||[univestiName isEqualToString:@""]) {
            //获取个人信息详情
            [self loadingUserInformationFunction];
        }else{
            self.tecSchoolLabel.text = univestiName;
        }
        
    }
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//获取用户个人信息详情
-(void) loadingUserInformationFunction{
     SINAUserModel *userModel = [[SINACommonUserHelper share] getUserModel];
    NSString *userUid=userModel.userUid;
    DLog(@"获取用户个人信息详情 uid is %@",userUid);
    if (userUid==nil||userUid.length<1) {
       
    } else {
        WeakSelf;
        [CXAFNHttpRequestUtils httpGetRequest:[NSString stringWithFormat:@"%@%@?uuid=%@",baseUrl,userInformationUrl,userUid]  success:^(id responseData) {
            NSString *code = responseData[@"code"];
            NSString *universityname=@"";

            if ([code isEqualToString:@"1000"]) {
                NSDictionary *personInfromation = responseData[@"content"];
                
                universityname=personInfromation[@"universityname"];
                userModel.universityName = universityname;
                if ([universityname isEqualToString:@""]||[universityname isEqualToString:@"(null)"]) {
                    weakSelf.tecSchoolLabel.text=@"暂无学校信息";
                }else{
                    [[SINACommonUserHelper share]saveUserModel:userModel];
                    weakSelf.tecSchoolLabel.text=universityname;
                }
                
            }
        } failure:^(NSError *error, NSInteger errCode) {
           
        }];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
